import {AddObjectCommand} from "../commands/Commands.js";

function Loader(editor) {
    this.import = async function(){
        let fileHandle;
        [fileHandle] = await window.showOpenFilePicker();
        var fileName = fileHandle.name;
        var extension = fileName.split('.').pop().toLowerCase();

        let file;
        let contents;
        switch (extension) {
            case '3dm':
                file = await fileHandle.getFile();
                contents = await file.arrayBuffer();
                var { Rhino3dmLoader } = await import( '../jsm/loaders/3DMLoader.js' );
                var loader = new Rhino3dmLoader();
                loader.setLibraryPath( './js/jsm/libs/rhino3dm/' );
                loader.parse(contents, function (object){
                    editor.execute(new AddObjectCommand(editor, object))
                });
                break;
            case 'obj':
                file = await fileHandle.getFile();
                contents = await file.text();

                var { OBJLoader } = await import( '../jsm/loaders/OBJLoader.js' );
                var object = new OBJLoader().parse( contents );
                object.name = fileName;
                editor.execute( new AddObjectCommand( editor, object ) );

                break;
        }
    }
}

export{Loader};
